layui.define(['table', 'form', 'admin', 'upload', 'element'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , element = layui.element
        , upload = layui.upload
        , form = layui.form
        , admin = layui.admin;

    var window_index = parent.layer.getFrameIndex(window.name);

    var elem_config = ["LAY-ads-applaunch-manage", 'LAY-ads-apphome-manage', 'LAY-ads-apphomepop-manage', 'LAY-ads-svideo-manage', 'LAY-ads-appmoments-manage'];
    var current_index = 0;
    element.on('tab(tabBrief)', function (data) {
        current_index = data.index;
        switch (current_index + 1) {
            case 1:
                table.render({
                    elem: "#" + elem_config[data.index]
                    , url: '/admin/security/getlist'
                    , where: {type: (data.index + 1)}
                    , cols: [[
                        {field: 'id', width: 100, title: 'ID', sort: true}
                        , {field: 'device', width: 200, title: '设备号'}
                        , {
                            field: '', width: 200, title: '曾注册账号', templet: function (d) {
                                return d.devices.accounts ? d.devices.accounts : '';
                            }
                        }
                        , {field: 'note', title: '备注'}
                        , {field: 'add_time', width: 200, title: '添加时间'}
                        , {field: 'modify_time', width: 200, title: '修改时间'}
                        , {
                            field: '', minWidth: 20, title: '修改人', templet: function (d) {
                                return d.admin ? d.admin.nick_name : ''
                            }
                        }
                        , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#table-adslist-1'}
                    ]]
                    , page: true
                    , limit: 100
                    , limits: [10, 20, 30, 50, 70, 80, 90, 100]
                    , height: 'full-140'
                    , text: '对不起，加载出现异常！'
                    , even: true
                });
                break;
            case 2:
                table.render({
                    elem: "#" + elem_config[data.index]
                    , url: '/admin/security/getlist'
                    , where: {type: (data.index + 1)}
                    , cols: [[
                        {field: 'id', width: 100, title: 'ID', sort: true}
                        , {field: 'ip', width: 200, title: 'IP地址'}
                        , {
                            field: '', width: 200, title: '曾登录账号', templet: function (d) {
                                return d.ips.accounts ? d.ips.accounts : '';
                            }
                        }
                        , {field: 'note', title: '备注'}
                        , {field: 'add_time', width: 200, title: '添加时间'}
                        , {field: 'modify_time', width: 200, title: '修改时间'}
                        , {
                            field: '', minWidth: 20, title: '修改人', templet: function (d) {
                                return d.admin ? d.admin.nick_name : ''
                            }
                        }
                        , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#table-adslist-2'}
                    ]]
                    , page: true
                    , limit: 100
                    , limits: [10, 20, 30, 50, 70, 80, 90, 100]
                    , height: 'full-140'
                    , text: '对不起，加载出现异常！'
                    , even: true
                });
                break;
            case 3:
                table.render({
                    elem: "#" + elem_config[data.index]
                    , url: '/admin/security/getlist'
                    , where: {type: (data.index + 1)}
                    , cols: [[
                        {field: 'id', width: 100, title: 'ID', sort: true}
                        , {field: 'bank', width: 200, title: '银行卡'}
                        , {
                            field: '', width: 200, title: '曾登录账号', templet: function (d) {
                                return d.devices ? d.devices.accounts : '';
                            }
                        }
                        , {field: 'note', title: '备注'}
                        , {field: 'add_time', width: 200, title: '添加时间'}
                        , {field: 'modify_time', width: 200, title: '修改时间'}
                        , {
                            field: '', width: 200, title: '修改人', templet: function (d) {
                                return d.admin ? d.admin.nick_name : ''
                            }
                        }
                        , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#table-adslist-3'}
                    ]]
                    , page: true
                    , limit: 100
                    , limits: [10, 20, 30, 50, 70, 80, 90, 100]
                    , height: 'full-140'
                    , text: '对不起，加载出现异常！'
                    , even: true
                });
                break;
            case 4:
                table.render({
                    elem: "#" + elem_config[data.index]
                    , url: '/admin/security/getlist'
                    , where: {type: (data.index + 1)}
                    , cols: [[
                        {field: 'id', width: 100, title: 'ID', sort: true}
                        , {field: 'keyword', width: 200, title: '屏蔽词'}
                        , {field: 'note', title: '备注'}
                        , {field: 'add_time', width: 200, title: '添加时间'}
                        , {field: 'modify_time', width: 200, title: '修改时间'}
                        , {
                            field: '', width: 200, title: '修改人', templet: function (d) {
                                return d.admin ? d.admin.nick_name : ''
                            }
                        }
                        , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#table-adslist-4'}
                    ]]
                    , page: true
                    , limit: 100
                    , limits: [10, 20, 30, 50, 70, 80, 90, 100]
                    , height: 'full-140'
                    , text: '对不起，加载出现异常！'
                    , even: true
                });
                break;
            case 5:
                table.render({
                    elem: "#" + elem_config[data.index]
                    , url: '/admin/security/getlist'
                    , where: {type: (data.index + 1)}
                    , cols: [[
                        {field: 'id', width: 100, title: 'ID', sort: true}
                        , {field: 'card', width: 200, title: '身份证号'}
                        , {field: 'card', width: 200, title: '曾登录账号'}
                        , {field: 'note', title: '备注'}
                        , {field: 'add_time', width: 200, title: '添加时间'}
                        , {field: 'modify_time', width: 200, title: '修改时间'}
                        , {
                            field: '', width: 200, title: '修改人', templet: function (d) {
                                return d.admin ? d.admin.nick_name : ''
                            }
                        }
                        , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#table-adslist-5'}
                    ]]
                    , page: true
                    , limit: 100
                    , limits: [10, 20, 30, 50, 70, 80, 90, 100]
                    , height: 'full-140'
                    , text: '对不起，加载出现异常！'
                    , even: true
                });
                break;
            default:

        }

    });
    //监听搜索
    form.on('submit(LAY-order-front-search)', function (data) {
        let id = $(this).attr('data-index');
        var field = data.field;
        //执行重载
        table.reload(elem_config[id], {
            where: field
            , page: {
                curr: 1   //默认从第一页开始搜索
            }
        });
    });
    //默认加载启动页广告
    table.render({
        elem: '#LAY-ads-applaunch-manage'
        , url: '/admin/security/getlist'
        , where: {type: 1}
        , cols: [[
            {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'device', width: 200, title: '设备号'}
            , {
                field: '', width: 200, title: '曾注册账号', templet: function (d) {
                    return d.devices.accounts ? d.devices.accounts : '';
                }
            }
            , {field: 'note', title: '备注'}
            , {field: 'add_time', width: 200, title: '添加时间'}
            , {field: 'modify_time', width: 200, title: '修改时间'}
            , {
                field: '', minWidth: 20, title: '修改人', templet: function (d) {
                    return d.admin ? d.admin.nick_name : '';
                }
            }
            , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#table-adslist-1'}
        ]]
        , page: true
        , limit: 100
        , limits: [10, 20, 30, 50, 70, 80, 90, 100]
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    // 添加
    form.on('submit(ads-add)', function (obj) {
        layer.load();
        admin.req({
            url: '/admin/security/appadd_post'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    parent.layer.close(window_index);
                    parent.layer.msg(res.msg, {icon: 1});
                    parent.layui.table.reload(elem_config[obj.field.type - 1]);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            },
        });
        return false;
    });

    // 编辑
    form.on('submit(ads-edit)', function (obj) {
        layer.load();
        admin.req({
            url: '/admin/security/appedit_post'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    parent.layer.close(window_index);
                    parent.layer.msg(res.msg, {icon: 1});
                    parent.layui.table.reload(elem_config[obj.field.type - 1]);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    //监听工具条
    table.on('tool', function (obj) {
        let $type = $(this).attr('data-dtype');
        if (obj.event === 'del') {
            layer.confirm('确认删除？', function (index) {
                layer.load();
                admin.req({
                    url: '/admin/security/appdel_post?type=' + $type
                    , data: {"id": obj.data.id}
                    , success(res) {
                        layer.closeAll("loading");
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            table.reload(elem_config[current_index]);
                        }
                    }
                    , complete: function () {
                        layer.closeAll("loading");
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '编辑'
                , content: '/admin/security/appedit?id=' + obj.data.id + '&type=' + $type
                , maxmin: true
                , area: ['650px', '400px']
            });
        }
    });
    exports('black', {})
});